package com.jiliason.segmentation.doMain.event;

import com.jiliason.segmentation.constant.Ct;
import com.jiliason.segmentation.constant.enums.Regex;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.IntStream;

import static com.jiliason.segmentation.constant.enums.TextureTag.*;

@Component
public class AddrTextureEngine {
    public String[] execute(List<String> addrFragments) {
        // 生成初始纹理
        String[] tags = initTexture(addrFragments);
        // 纹理list合理性处理
        int len = tags.length;
        String[] mod = new String[len];

        // 1.独字，非other，非name_mul
        loneWord(tags, mod, len);

        // 2.一部分多字情况
        pairingWord(tags, mod, len);

        for (int i = 0; i < len; i++) {
            if (Xiu.v().equals(mod[i])) {
                tags[i] = mod[i];
            }
        }

        return tags;
    }

    private String[] initTexture(List<String> addrFragments) {
        int len = addrFragments.size();
        String[] tags = new String[len];

        for (int i = 0; i < len; i++) {
            String frg = addrFragments.get(i);
            String tag = getTag(frg);
            tags[i] = tag;
        }
        return tags;
    }

    private String getTag(String frg) {
        // 需要合并的碎片对象（单个的中文数字，单个的阿拉伯数字，单个不是type的字，单个的[东南西北米]）
        String tag;
        if (frg.matches(Regex.cr4.v())) {
            tag = DNXB.v();
        } else if (frg.matches(Regex.arDescription.v())) {
            tag = Other.v();
        } else if (frg.matches(Regex.cr1.v())) {
            tag = frg.length() == 1 ? Digit_ALB_S.v() : Digit_ALB_M.v();
        } else if (frg.matches(Regex.cr2.v())) {
            tag = frg.length() == 1 ? Letter_S.v() : Letter_M.v();
        } else if (frg.matches(Regex.cr3.v())) {
            tag = frg.length() == 1 ? Digit_CNJ_S.v() : Digit_CNJ_M.v();
        } else if (frg.matches(Regex.ar5.v())) {
            tag = frg.length() == 1 ? Types_Road_S.v() : Types_Road_M.v();
        } else if (frg.matches(Regex.ar6.v())) {
            tag = frg.length() == 1 ? Types_Poi_S.v() : Types_Poi_M.v();
        } else if (frg.matches(Regex.ar7.v())) {
            tag = frg.length() == 1 ? Types_Detail_S.v() : Types_Detail_M.v();
        } else if (frg.matches(Regex.arPt.v())) {
            tag = frg.length() == 1 ? Types_S.v() : Types_M.v();
        } else {
            tag = frg.length() == 1 ? Name_S.v() : Name_M.v();
        }
        return tag;
    }

    private void loneWord(String[] tags, String[] mod, int len) {
        Integer[] indexes = IntStream.range(0, len)
                .filter(i -> tags[i].equals(DNXB.v())
                        || (tags[i].endsWith(S.v()) && !tags[i].startsWith(Name.v())))
                .boxed().toArray(Integer[]::new);
        // 看1前1后策略
        for (int i : indexes) {
            String bef_1 = i - 1 < 0 ? Ey.v() : tags[i - 1];
            String aft_1 = i + 1 >= len ? Ey.v() : tags[i + 1];
            String code =  bef_1 + "_" + tags[i] + "_" + aft_1;
            if (Ct.loneRules.stream().anyMatch(code::matches)) {
                mod[i] = Xiu.v();
            }
        }
    }

    private void pairingWord(String[] tags, String[] mod, int len) {
        Integer[] indexes = IntStream.range(0, len)
                .filter(i -> tags[i].endsWith(M.v()) && !tags[i].startsWith(Name.v()))
                .boxed().toArray(Integer[]::new);

        for (int i : indexes) {
            String bef = i - 1 < 0 ? "empty" : tags[i - 1];
            String aft = i + 1 >= len ? "empty" : tags[i +1];
            String code = bef + "_" + aft;
            if (Ct.pairingRules.stream().anyMatch(code::matches)) {
                mod[i] = Xiu.v();
            }
        }
    }
}
